<?php
# get the diagram root list
/*
获取网站架构图的跟目录列表, 是直接打印出来的
*/
function get_diagram_root_list($args='') {
    global $html;
    $default = array('index'=>'/', 'ul_class'=>'', 'li_class'=>'', 'li_selected_class'=>'selected', 'a_class'=>'', 'a_selected_class'=>'');
    parse_str($args, $array);
    $init = $array + $default;
    $current_url = THIS_URL;
    
    /* get the curent diagram parent list */

    if (!empty($current_url)) {
        $current_diagram = get_diagram_by_url($current_url);
        $parent_list = get_diagram_parent_list_by_child($current_diagram->id);
    } else {
        $parent_list = array();
    }

    $diagram_items = get_diagram_childs_by_id(0);
    $output = !empty($init['ul_class']) ? '<ul class="' . $init['ul_class'] . '">' : '<ul>';
    foreach($diagram_items as $diagram_item) {
        $a_class = $init['a_class'];
        $li_class = $init['li_class'];
        $url = $diagram_item->url;
        # for the index page
        if ($diagram_item->url == $init['index']) {
            $url = '/';
            if (empty($current_url)) {
                $a_class = $init['a_selected_class'];
                $li_class = $init['li_selected_class'];
            }
        # for the current diagram
        } else if (in_array($diagram_item->id, $parent_list)) {
            $a_class = $init['a_selected_class'];
            $li_class = $init['li_selected_class'];
        }
        $output .= !empty($li_class) ? '<li class="' . $li_class . '">' : '<li>';
        $a_array = !empty($a_class) ? array('class'=>$a_class) : array();
        $output .= $html->link($diagram_item->title, $url, $a_array) . '</li>';
    }
    $output .= '</ul>';
    echo $output;
    return null;
}

# get the current diagram menu
function current_sub_menu_list($args = '') {
    global $diagram_cache, $init;
    $default = array('index'=>'index', 'ul_class'=>'', 'item_class'=>'item', 'item_selected_class'=>'', 'li_selected_class'=>'selected',  'indentation'=>true);
    parse_str($args, $array);
    $init = $array + $default;

    $root_diagram = get_diagram_root_by_url(THIS_URL);
    arlicle_components('diagram');
    $diagram_cache = cache_current_diagram();
    $output = '';
    get_sub_menu_list($root_diagram);
}

/*
获取Diagram的title
1 可以传入diagram的id或者diagram的 url
2 如果diagram的id或者diagram的url都为空,则自动用 THIS_URL来进行获取
因为item的url可能会和page或者category的相同,因此获取的时候要注意,如果是明确获取某一个的话,直接用对应的获取方法: page_title, item_title, category_title, url_title
*/
function diagram_title($url = null) {
    echo _diagram_title($url);
}

function _diagram_title($url = null) {
    global $aldb;
    if (empty($url)) {
        $url = THIS_URL;
    }
    $id = (int)$url;
    if (empty($id) || $id != $url) {
        $condition = "url='$url'";
    } else {
        $condition = "id='$id'";
    }
    return $aldb->get_var("SELECT title FROM $aldb->diagram WHERE $condition");
}

/*
获取Page 的 title
1 可以传入page的id或者page的 url
2 如果page的id或者page的url都为空,则自动用 THIS_URL来进行获取
3 直接打印title
*/
function page_title($url = '') {
    echo _page_title($url);
}

/*
获取Page 的 title
1 可以传入page的id或者page的 url
2 如果page的id或者page的url都为空,则自动用 THIS_URL来进行获取
3 不打印title
*/
function _page_title($url = '') {
    $diagram = get_diagram_by_url($url, 'page', 'title');
    if (!empty($diagram)) {
        return $diagram->title;
    }
}

/*
获取Page 的 content
1 可以传入page的id或者page的 url
2 如果page的id或者page的url都为空,则自动用 THIS_URL来进行获取
3 不打印content
*/
function page_content($url = '') {
    echo _page_content($url);
}
/*
获取Page 的 content
1 可以传入page的id或者page的 url
2 如果page的id或者page的url都为空,则自动用 THIS_URL来进行获取
3 不打印content
*/
function _page_content($url = '') {
    global $html;
    $diagram = get_diagram_by_url($url, 'page', array('content', 'id', 'url'));
    $content = '';
    if (!empty($diagram)) {
        $content = $diagram->content;
        # check is is admin and can edit this page
        if (current_user_can('diagram', 'manage')) {
            $content .= '<p>' . $html->admin_link(__('Edit'), "/diagram/page_edit/$diagram->id/edit_from_web/?" . THIS_URL, array('style'=>'color:#ff0000;')) . '</p>';
        }
    }
    return $content;
}

/*
item title
*/
function item_title($url = '') {
    echo _item_title($url);
}

function _item_title($url = '') {
    $diagram = get_diagram_by_url($url, 'item', 'title');
    if (!empty($diagram)) {
        return $diagram->title;
    }
}

/*
category title
*/
function category_title($url = '') {
    echo _category_title($url);
}

function _category_title($url = '') {
    $diagram = get_diagram_by_url($url, 'list', 'title');
    if (!empty($diagram)) {
        return $diagram->title;
    }
}

/*
category title
*/
function url_title($url = '') {
    echo _url_title($url);
}

function _url_title($url = '') {
    $diagram = get_diagram_by_url($url, 'url', 'title');
    if (!empty($diagram)) {
        return $diagram->title;
    }
}

# get he diagram all childs list by url, include self id
function get_diagram_all_childs_list_by_url($url) {
    $diagram = get_diagram_by_url($url);
    if (!empty($diagram)) {
        $childs_list = get_diagram_childs_list_by_id($diagram->id);
        $childs_list .= ',' . $diagram->id;
        $childs_list = trim($childs_list, ',');
        return $childs_list;
    }
}

# parse the template params
function parse_template_params($url, $key) {
    global $template_params;
    $params_url = substr($url, strlen($key));
    $params_url = trim($params_url, '/');
    $params_array = explode('/', $params_url);
    $params = array();
    $template_params = array_filter($params_array, "strlen");
}

# get the template params
function params($key) {
    global $template_params;
    $key = $key - 1;
    if ($key < 0) {
        $key = 0;
    }

    if (isset($template_params[$key])) {
        return $template_params[$key];
    }
}

/*
获取分类列表和文章列表


*/

function get_categories_and_posts_list($settings = array()) {
    $default_params = array('url'=>'', 'type'=>'default', 'limit'=>5);
    $settings += $default_params;
    $categories = get_diagram_childs_by_url($settings['url']);

    $output = '';
    if (!empty($categories)) {
        foreach($categories as $category) {
            $output .= "<div class=\"category_title_item\" id=\"category_title_item_$category->id\"><div class=\"category_title\" id=\"category_title_$category->id\"><a href=\"" . arlicle_url($category->url) . "\" >$category->title</a></div><div class=\"category_more\" id=\"category_more_$category->id\"><a href=\"" . arlicle_url($category->url) . "\" >" . __('More') ."</a></div></div>\n";
            $output .= "<div class=\"posts_list\" id=\"posts_list_$category->id\">\n";
            $output .= _get_list($category);
            $output .= '</div>' . "\n";
        }
    }
    echo $output;
}


/*
$settings 中可以设置是图片列表还是文字列表
*/
function get_category_and_posts_list($settings = array()) {
    $default_params = array('url'=>null, 'type'=>'');
    $settings += $default_params;
    $category = get_diagram_by_url($settings['url'], 'list');
    $list_type = '';
    if (empty($category)) {
        return '';
    }
    $parameter['category'] = $category;
    if (empty($settings['type'])) {
        $list_type = get_diagram_metavalue('list_type', $category->metavalue);
    } else {
        $list_type = $settings['type'];
    }
    $output = '<div class="category_title">' . $category->title . '</div>' . "\n";
    $output .= '<div class="posts_list">' . "\n";

    switch($list_type) {
        case 'images':
            $output .= _get_images_list($parameter);
            break;
        default:
        case 'news':
            $output .= _get_news_list($parameter);
            break;
    }
    $output .= '</div>' . "\n";
    $output .= al_paginate(array('perpage'=>10, 'paginate_style' => 'paginate_1'));
    echo $output;
}

function show_post($slug = '') {
    global $html;
    if (empty($slug)) {
        $slug = params(2);
    }
    $post = get_post($slug);
    if (!empty($post)) {
        $content = apply_filters('the_content', $post->post_content);
        # check is is admin and can edit this page
        if (current_user_can('Post', 'manage')) {
            $content .= '<p>' . $html->admin_link(__('Edit'), "/post_new/post_new/$post->id/edit/?" . THIS_URL . "post/$post->id/", array('style'=>'color:#ff0000;')) . '</p>';
        }
        echo "<div class=\"post_item\" id=\"post_item_$post->id\">";
        echo "<div class=\"post_title_item\" id=\"post_title_item_$post->id\">";
        echo "<div class=\"post_title\" id=\"post_title_$post->id\">$post->post_title</div>";
        echo "<div class=\"post_time\" id=\"post_time_$post->id\"><span class=\"post_time_title\">" . __('Post time') . "</span>" . time_format($post->post_time). "</div>";
        echo "<div class=\"post_author\" id=\"post_author_$post->id\"><span class=\"post_author_title\">" . __('Post by') . "</span>" . get_username_by_id($post->poster_id). "</div></div>";
        echo "<div class=\"post_content\" id=\"post_content_$post->id\">" . $content . '</div>';
        echo "</div>";
    } else {
        error_404(false);
    }
}

function global_site_title() {
    if (params(1) == 'post') {
        $title = _post_title();
    } else {
        $title = _diagram_title();
    }
    echo $title;
    if (!empty($title)) {
        echo ' - ';
    }
    site_title();
}
?>
